package com.springboot.swagger2.web;

import com.springboot.swagger2.model.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author liuwei
 * @version 2017/7/15 下午6:28
 * @modified TODO
 * @description 功能描述
 * @see
 * @since 1.0
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
    private Map<Long , User> userMap = new ConcurrentHashMap<Long, User>();

    @ApiOperation(value = "用户列表" ,notes = "")
    @RequestMapping(value = "/getList" , method = RequestMethod.GET)
    public List<User> getAll(){
        return new ArrayList<User>(userMap.values());
    }

    @ApiOperation(value = "添加用户" ,notes = "")
    @RequestMapping(value = "/save" , method = RequestMethod.POST)
    public String save(@ModelAttribute User user){
        userMap.put(user.getId() , user);
        return "success";
    }

    @ApiOperation(value = "添加用户" ,notes = "")
    @ApiImplicitParam(name = "user" ,value = "用户实体", required = true , dataType = "User")
    @RequestMapping(value = "/saveByJson" , method = RequestMethod.POST)
    public String saveByJson(@RequestBody User user){
        userMap.put(user.getId() , user);
        return "success";
    }

    @ApiOperation(value = "查询用户" ,notes = "")
    @ApiImplicitParam(name = "id" , required = true ,value = "用户id" , dataType = "Long")
    @RequestMapping(value = "/{id}" , method = RequestMethod.GET)
    public User getById(@PathVariable Long id){
        return userMap.get(id);
    }

    @ApiOperation(value = "删除用户" ,notes = "" , httpMethod = "delete")
    @ApiImplicitParam(name = "id" , required = true ,value = "用户id" , dataType = "Long")
    @RequestMapping(value = "/{id}" , method = RequestMethod.DELETE)
    public void remove(@PathVariable Long id){
        userMap.remove(id);
    }

    @ApiOperation(value = "添加用户" ,notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , required = true ,value = "用户id" , dataType = "Long"),
            @ApiImplicitParam(name = "userName" , required = true ,value = "姓名" , dataType = "String")
    })
    @RequestMapping(value = "/save/{id}/{userName}" , method = RequestMethod.PUT)
    public String put(@PathVariable Long id , @PathVariable String userName){
        User user = new User();
        user.setId(id);
        user.setUserName(userName);
        userMap.put(id , user);
        return "success";
    }
}
